﻿Public Class fCreateItem

    Dim profont9 As Font
    Dim txtbrush(4) As Brush

    Dim tmpID_ITEMBASE As Int64

    Private Sub fCreateItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        txtName.MaxLength = Protocol.MAX_ITEMNAME_LEN

        'Item Types
        cmbType.BeginUpdate()
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 0))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 1))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 2))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 3))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 4))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 5))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 6))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 7))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 8))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 9))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 10))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 11))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 12))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 13))
        'cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), 14))
        Dim types() As Protocol.ItemType = System.Enum.GetValues(GetType(Protocol.ItemType))
        For Each type As Protocol.ItemType In types
            cmbType.Items.Add(System.Enum.GetName(GetType(Protocol.ItemType), type))
        Next

        '(Protocol.ItemType)
        cmbType.EndUpdate()

        'Item rarity classes
        cmbItemClass.BeginUpdate()
        'cmbItemClass.Items.Add(System.Enum.GetName(GetType(Protocol.ItemClass), 0))
        'cmbItemClass.Items.Add(System.Enum.GetName(GetType(Protocol.ItemClass), 1))
        'cmbItemClass.Items.Add(System.Enum.GetName(GetType(Protocol.ItemClass), 2))
        'cmbItemClass.Items.Add(System.Enum.GetName(GetType(Protocol.ItemClass), 3))
        Dim classtypes() As Protocol.ItemClass = System.Enum.GetValues(GetType(Protocol.ItemClass))
        For Each type As Protocol.ItemClass In classtypes
            cmbItemClass.Items.Add(System.Enum.GetName(GetType(Protocol.ItemClass), type))
        Next
        cmbItemClass.EndUpdate()

        profont9 = New System.Drawing.Font(New FontFamily("ProFontWindows"), 9.0F)

        txtbrush(0) = New SolidBrush(Color.Gray)
        txtbrush(1) = New SolidBrush(Color.White)
        txtbrush(2) = New SolidBrush(Color.LimeGreen)
        txtbrush(3) = New SolidBrush(Color.Orchid)
        txtbrush(4) = New SolidBrush(Color.Black)

        numLooks.Value = 1

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Main.frmManager.LoadItemBaseTab(tmpID_ITEMBASE)
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If txtName.Text = "" Then
            MsgBox("Please enter a name for the item!", MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly)
            Exit Sub
        ElseIf cmbType.SelectedItem Is Nothing Then
            MsgBox("Please select an item type!", MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly)
            Exit Sub
        ElseIf numLooks.Value = 0 Then
            MsgBox("Please select an item look!", MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly)
            Exit Sub
        ElseIf cmbItemClass.SelectedItem Is Nothing Then
            MsgBox("Please select an item class!", MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly)
            Exit Sub
        Else

            'save the item (new item/update existing)
            If tmpID_ITEMBASE > 0 Then

                DatabaseManager.UpdateItem(tmpID_ITEMBASE, txtName.Text, cmbType.SelectedIndex, numShape.Value, numLooks.Value, chkStacks.Checked, numStdItem.Value, numLevel.Value, numWeight.Value, _
                                           cmbItemClass.SelectedIndex, numXPBonus.Value, numMinAC.Value, numMinMAC.Value, numMinDC.Value, numMinMC.Value, numMinSC.Value, numHP.Value, _
                                           numMP.Value, numMaxAC.Value, numMaxMAC.Value, numMaxDC.Value, numMaxMC.Value, numMaxSC.Value, numBagWeight.Value, numHandWeight.Value, _
                                           numBodyWeight.Value, numFireMC.Value, numIceMC.Value, numLightMC.Value, numWindMC.Value, numHolyMC.Value, numDarkMC.Value, _
                                           numPhantomMC.Value, numFireMAC.Value, numIceMAC.Value, numLightMAC.Value, numWindMAC.Value, numHolyMAC.Value, numDarkMAC.Value, _
                                           numPhantomMAC.Value, numAccuracy.Value, numAgility.Value, numVelocity.Value, numDura.Value, numStrength.Value, numLuck.Value)

            Else

                DatabaseManager.CreateItem(txtName.Text, cmbType.SelectedIndex, numShape.Value, numLooks.Value, chkStacks.Checked, numCost.Value, numStdItem.Value, numLevel.Value, numWeight.Value, _
                                           cmbItemClass.SelectedIndex, numXPBonus.Value, numMinAC.Value, numMinMAC.Value, numMinDC.Value, numMinMC.Value, numMinSC.Value, numHP.Value, _
                                           numMP.Value, numMaxAC.Value, numMaxMAC.Value, numMaxDC.Value, numMaxMC.Value, numMaxSC.Value, numBagWeight.Value, numHandWeight.Value, _
                                           numBodyWeight.Value, numFireMC.Value, numIceMC.Value, numLightMC.Value, numWindMC.Value, numHolyMC.Value, numDarkMC.Value, _
                                           numPhantomMC.Value, numFireMAC.Value, numIceMAC.Value, numLightMAC.Value, numWindMAC.Value, numHolyMAC.Value, numDarkMAC.Value, _
                                           numPhantomMAC.Value, numAccuracy.Value, numAgility.Value, numVelocity.Value, numDura.Value, numStrength.Value, numLuck.Value)

            End If

            'reload the itembase
            Main.frmManager.LoadItemBaseTab(tmpID_ITEMBASE)
            'close this form
            Me.Close()

        End If

    End Sub

    Public Sub LoadItem(id As Int64, Optional savetosameid As Boolean = True)

        If savetosameid Then tmpID_ITEMBASE = id

        Dim sSql As String = "SELECT * FROM TBL_ITEMBASE WHERE ID_ITEMBASE='" & id & "';"
        Dim row As Collection = DatabaseManager.DB_Game.ExecuteQueryFirstRecord(sSql)

        If row Is Nothing Then
            Me.Close()
            Exit Sub
        End If

        If row.Count = 0 Then
            Me.Close()
            Exit Sub
        End If

        txtName.Text = row("STR_NAME")
        cmbType.Text = CType(row("BYTE_TYPE"), ItemType).ToString
        numShape.Value = row("INT_SHAPE")
        numLooks.Value = row("INT_LOOKS")
        chkStacks.Checked = row("BOOL_STACKS")
        numCost.Value = row("INT8_COST")
        numStdItem.Value = row("INT_STDITEM")
        numLevel.Value = row("INT2_LEVELREQ")
        numWeight.Value = row("INT2_WEIGHT")
        cmbItemClass.Text = CType(row("BYTE_ITEMCLASS"), ItemClass).ToString
        numXPBonus.Value = row("INT1_EXPBONUS")
        numMinAC.Value = row("INT2_MIN_AC")
        numMinMAC.Value = row("INT2_MIN_MAC")
        numMinDC.Value = row("INT2_MIN_DC")
        numMinMC.Value = row("INT2_MIN_MC")
        numMinSC.Value = row("INT2_MIN_SC")
        numHP.Value = row("INT2_MAX_HP")
        numMP.Value = row("INT2_MAX_MP")
        numMaxAC.Value = row("INT2_MAX_AC")
        numMaxMAC.Value = row("INT2_MAX_MAC")
        numMaxDC.Value = row("INT2_MAX_DC")
        numMaxMC.Value = row("INT2_MAX_MC")
        numMaxSC.Value = row("INT2_MAX_SC")
        numBagWeight.Value = row("INT2_MAX_WEIGHT")
        numHandWeight.Value = row("INT2_MAX_HANDWEIGHT")
        numBodyWeight.Value = row("INT2_MAX_WEARWEIGHT")
        numFireMC.Value = row("INT2_ELEMC_FIRE")
        numIceMC.Value = row("INT2_ELEMC_ICE")
        numLightMC.Value = row("INT2_ELEMC_LIGHT")
        numWindMC.Value = row("INT2_ELEMC_WIND")
        numHolyMC.Value = row("INT2_ELEMC_HOLY")
        numDarkMC.Value = row("INT2_ELEMC_DARK")
        numPhantomMC.Value = row("INT2_ELEMC_PHANTOM")
        numFireMAC.Value = row("INT2_ELEMAC_FIRE")
        numIceMAC.Value = row("INT2_ELEMAC_ICE")
        numLightMAC.Value = row("INT2_ELEMAC_LIGHT")
        numWindMAC.Value = row("INT2_ELEMAC_WIND")
        numHolyMAC.Value = row("INT2_ELEMAC_HOLY")
        numDarkMAC.Value = row("INT2_ELEMAC_DARK")
        numPhantomMAC.Value = row("INT2_ELEMAC_PHANTOM")
        numAccuracy.Value = row("INT1_ACCURACY")
        numAgility.Value = row("INT1_AGILITY")
        numVelocity.Value = row("INT1_VELOCITY")
        numDura.Value = row("INT_MAX_DURABILITY")
        numStrength.Value = row("INT1_STRENGTH")
        numLuck.Value = row("INT1_LUCK")

    End Sub

    Private prevValue As Integer

    Private Sub numLooks_ValueChanged(sender As Object, e As EventArgs) Handles numLooks.ValueChanged

        Dim idxstep As Integer = numLooks.Value - prevValue
        If idxstep = -1 Or idxstep = 1 Then

            If numLooks.Value >= 0 And Libs.Inventory.ImageCount >= numLooks.Value Then

                If Libs.Inventory.Image(numLooks.Value).HasImage = False Then

                    If numLooks.Value + idxstep <= 0 Then

                        prevValue = Libs.Inventory.ImageCount + 1
                        numLooks.Value = Libs.Inventory.ImageCount

                    ElseIf numLooks.Value + idxstep > Libs.Inventory.ImageCount Then

                        prevValue = -1
                        numLooks.Value = 0

                    Else

                        prevValue = numLooks.Value
                        numLooks.Value += idxstep

                    End If

                    Exit Sub

                End If

            End If

        End If

        prevValue = numLooks.Value

        Dim buff As Bitmap = New Bitmap(pbPreview.Width, pbPreview.Height)
        Dim g As Graphics = Graphics.FromImage(buff)

        g.Clear(SystemColors.Control)

        If numLooks.Value > 0 And Libs.Inventory.ImageCount >= numLooks.Value Then

            If Libs.Inventory.Image(numLooks.Value).HasImage Then

                With Libs.Inventory.Image(numLooks.Value)

                    g.DrawImage(.Bitmap, 8, 8)

                    If chkStacks.Checked Then

                        Dim previewStack As String = "99"

                        g.DrawString(previewStack, profont9, txtbrush(4), 7, 8)
                        g.DrawString(previewStack, profont9, txtbrush(4), 9, 8)
                        g.DrawString(previewStack, profont9, txtbrush(4), 8, 7)
                        g.DrawString(previewStack, profont9, txtbrush(4), 8, 9)

                        g.DrawString(previewStack, profont9, txtbrush(1), 8, 8)

                    End If

                End With

                If Not Libs.Ground.Image(numLooks.Value) Is Nothing Then
                    With Libs.Ground.Image(numLooks.Value)

                        If .HasImage Then

                            Dim x As Integer = pbPreview.ClientSize.Width - CInt(Editor.TileWidth * 1.5F) - 8
                            Dim y As Integer = 8

                            g.DrawImage(.Bitmap, CInt((x + (Editor.TileWidth / 2)) - (.Header.Width / 2)), CInt((y + (Editor.TileHeight / 2)) - (.Header.Height / 2)))

                            Dim tW As Integer = (6 * txtName.Text.Length) + 4

                            x += (Editor.TileWidth / 2) - (tW / 2)

                            g.DrawString(txtName.Text, profont9, txtbrush(4), x - 1, y)
                            g.DrawString(txtName.Text, profont9, txtbrush(4), x + 1, y)
                            g.DrawString(txtName.Text, profont9, txtbrush(4), x, y - 1)
                            g.DrawString(txtName.Text, profont9, txtbrush(4), x, y + 1)

                            Dim idx As Integer
                            If cmbItemClass.SelectedItem Is Nothing Then
                                idx = 1
                            Else
                                idx = cmbItemClass.SelectedIndex
                                If idx < 0 Then idx = 0
                                If idx > 3 Then idx = 3
                            End If

                            g.DrawString(txtName.Text, profont9, txtbrush(idx), x, y)

                        End If

                    End With
                End If

            End If

        End If

        pbPreview.Image = buff

    End Sub

    Private Sub txtName_TextChanged(sender As Object, e As EventArgs) Handles txtName.TextChanged
        Call numLooks_ValueChanged(sender, e)
    End Sub

    Private Sub cmbItemClass_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbItemClass.SelectedIndexChanged
        Call numLooks_ValueChanged(sender, e)
    End Sub

    Private Sub chkStacks_CheckedChanged(sender As Object, e As EventArgs) Handles chkStacks.CheckedChanged
        Call numLooks_ValueChanged(sender, e)
    End Sub

End Class